import { Component, OnDestroy } from '@angular/core';

import { Util } from '../../../utils/util';

import { WordDetailProductService } from './word.product.service';

import { ToastyService } from '../../../modules/ng2-toasty';

import { ProductBase } from '../../product/product.base';

@Component({
  selector: 'word-product',
  templateUrl: './word.product.html',
  styles: [`
  `],
  providers: [WordDetailProductService]
})


export class WordDetailProductComponent extends ProductBase {

  // 参数
  param: any = {
    catName:'',
    categoryId: '',
    q: '',
    start: 0,
    rows: 10,
    sort: 'sellMoney',
    sortType: 'desc'
  };

  wordInfo: any;

  constructor(public util: Util, public service: WordDetailProductService,public toastyService:ToastyService) {
    super(service,util,toastyService);

    this.wordInfo = this.util.sessionStore().get('wordDetail');

    this.param.categoryId = this.wordInfo.categoryId;

    this.param.catName = this.wordInfo.categoryName;

    this.param.q = this.wordInfo.words;

    this.getHttp(this.param);
  }

  ngOnDestroy() {

    if (this.busy) {
      this.busy.unsubscribe();
    }

    if(this.busy2){
      this.busy2.unsubscribe();
    }
  }
   // 店铺类型切换
  shopType:any='';
  // shopType Change
  shopTypeChange(data) {

    console.log(data);

    if (this.param.shopType === data) {
      return;
    }

    this.param.shopType = data;

    this.getHttp(this.param);

  }

  //
  searchText:any;
  search(){
    if(this.searchText == this.param.q){
      return;
    }
    this.searchText = this.param.q;
    this.getHttp(this.param);
  }



  busy2:any;
  export(){

    let json = Object.assign({},this.param);

    json.rows = 1000;
    json.start = 0;

    // toast
    let toastOptions = {
      title: "",
      msg: "下载中...",
      showClose: true,
      timeout: 6000,
      theme: 'material'
    };
    // Add see all possible types in one shot
    this.toastyService.info(toastOptions);

    this.busy2 = this.service.getProduct(json).subscribe((res: any) => {


      let dim = ['date','numberId','title','picUrl','loc','propertyValue','brandName','price','monthPrice','monthSellAmount','monthSellMoney','sellAmount','sellMoney','category','catName','uid','shopName','shopType'];

      let dimText = ['时间','宝贝Id','宝贝标题','宝贝图片链接','地址','属性','品牌名','成交均价','月成交均价','月销售量','月销售额','销售量','销售额','类目id','类目','店铺id','店铺名','店铺类型'];

      let fileName = this.wordInfo.words+json.catName+json.shopType+"全部宝贝";


      let data = this.util.jsonToArrry(res.data.value, dim,  dimText);

      this.util.export(data, fileName);


    }, (err: any) => {
      this.busy2.unsubscribe();
    });
  }

}
